package com.wf.atm.client;


import com.wf.atm.exception.RegisterException;
import com.wf.atm.pojo.Account;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.IOException;

/**
 * @author Brainrain
 */
public class RegisterPanel extends JPanel {

    private ATMClient atmClient;


    public RegisterPanel(ATMClient atmClient) {
        this.atmClient = atmClient;
        initComponents();
    }

    //提交注册表单
    private void submitButtonActionPerformed(ActionEvent e) {

        Account act=null;
        try {
            //收集注册表单中的数据
            String name = nameTextField.getText();
            String password = new String(passwordField1.getPassword());
            String rePassword = new String(passwordField2.getPassword());
            int type = comboBox.getSelectedIndex();
            String personId = personIdTextField.getText();
            String address = addressTextField.getText();
            String email = emailTextField.getText();

            atmClient.printWriter.println("register," + name + "," + password + "," + rePassword + "," + personId + "," + address + "," + email + "," + type);
            atmClient.printWriter.flush();
            Object object = atmClient.ois.readObject();

            if(object instanceof  Account)
            {
                act=(Account) object;
            }
            else
            {
                JOptionPane.showMessageDialog(null,object.toString());
            }

            atmClient.businessPanel.initBusiness(act);


        } catch (IOException | ClassNotFoundException ex) {
            ex.printStackTrace();
            //显示弹窗
            JOptionPane.showMessageDialog(null,ex.getMessage());
        }
        if(act!=null) {
            //跳转至BusinessPanel页面
            CardLayout cardLayout = (CardLayout) this.getParent().getLayout();
            cardLayout.show(this.getParent(), "businessPanel");

        }
        //清空注册界面数据
        clearRegister();

    }

    //返回主界面
    private void backButtonActionPerformed(ActionEvent e) {
        CardLayout cardLayout = (CardLayout) this.getParent().getLayout();
        cardLayout.show(this.getParent(), "mainPanel");

        //清空注册界面
        clearRegister();
    }

    //清空注册表单
    public void clearRegister() {
        nameTextField.setText("");
        passwordField1.setText("");
        passwordField2.setText("");
        comboBox.setSelectedIndex(0);
        personIdTextField.setText("");
        addressTextField.setText("");
        emailTextField.setText("");
    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        title = new JLabel();
        typeLabel = new JLabel();
        nameLabel = new JLabel();
        pwdLabel = new JLabel();
        repwdLabel = new JLabel();
        personIdLabel = new JLabel();
        addressLabel = new JLabel();
        emailLabel = new JLabel();
        nameTextField = new JTextField();
        passwordField1 = new JPasswordField();
        passwordField2 = new JPasswordField();
        comboBox = new JComboBox<>();
        personIdTextField = new JTextField();
        addressTextField = new JTextField();
        emailTextField = new JTextField();
        submitButton = new JButton();
        backButton = new JButton();

        //======== this ========
        setLayout(null);

        //---- title ----
        title.setText("注 册 界 面");
        title.setFont(new Font("Microsoft YaHei", Font.PLAIN, 24));
        title.setHorizontalAlignment(SwingConstants.CENTER);
        add(title);
        title.setBounds(115, 65, 245, 35);

        //---- typeLabel ----
        typeLabel.setText("账户类型");
        typeLabel.setHorizontalAlignment(SwingConstants.CENTER);
        add(typeLabel);
        typeLabel.setBounds(55, 135, 105, 32);

        //---- nameLabel ----
        nameLabel.setText("姓 名");
        nameLabel.setHorizontalAlignment(SwingConstants.CENTER);
        add(nameLabel);
        nameLabel.setBounds(55, 190, 105, 32);

        //---- pwdLabel ----
        pwdLabel.setText("密    码");
        pwdLabel.setHorizontalAlignment(SwingConstants.CENTER);
        add(pwdLabel);
        pwdLabel.setBounds(55, 245, 105, 32);

        //---- repwdLabel ----
        repwdLabel.setText("确 认 密 码");
        repwdLabel.setHorizontalAlignment(SwingConstants.CENTER);
        add(repwdLabel);
        repwdLabel.setBounds(55, 300, 105, 32);

        //---- personIdLabel ----
        personIdLabel.setText("身 份 证 号");
        personIdLabel.setHorizontalAlignment(SwingConstants.CENTER);
        add(personIdLabel);
        personIdLabel.setBounds(55, 355, 105, 32);

        //---- addressLabel ----
        addressLabel.setText("详 细 地 址");
        addressLabel.setHorizontalAlignment(SwingConstants.CENTER);
        add(addressLabel);
        addressLabel.setBounds(55, 415, 105, 32);

        //---- emailLabel ----
        emailLabel.setText("邮    箱");
        emailLabel.setHorizontalAlignment(SwingConstants.CENTER);
        add(emailLabel);
        emailLabel.setBounds(55, 470, 105, 32);
        add(nameTextField);
        nameTextField.setBounds(185, 195, 190, 30);
        add(passwordField1);
        passwordField1.setBounds(185, 250, 190, 30);
        add(passwordField2);
        passwordField2.setBounds(185, 305, 190, 30);

        //---- comboBox ----
        comboBox.setModel(new DefaultComboBoxModel<>(new String[]{
                "储蓄账户",
                "信用账户",
                "可贷款的储蓄账户",
                "可贷款的信用账户"
        }));
        add(comboBox);
        comboBox.setBounds(185, 135, 190, 30);
        add(personIdTextField);
        personIdTextField.setBounds(185, 360, 190, 30);
        add(addressTextField);
        addressTextField.setBounds(185, 415, 190, 30);
        add(emailTextField);
        emailTextField.setBounds(185, 470, 190, 30);

        //---- submitButton ----
        submitButton.setText("确认");
        submitButton.addActionListener(e -> submitButtonActionPerformed(e));
        add(submitButton);
        submitButton.setBounds(115, 550, 92, 30);

        //---- backButton ----
        backButton.setText("返回");
        backButton.addActionListener(e -> backButtonActionPerformed(e));
        add(backButton);
        backButton.setBounds(280, 550, 92, 30);

        setPreferredSize(new Dimension(500, 650));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel title;
    private JLabel typeLabel;
    private JLabel nameLabel;
    private JLabel pwdLabel;
    private JLabel repwdLabel;
    private JLabel personIdLabel;
    private JLabel addressLabel;
    private JLabel emailLabel;
    private JTextField nameTextField;
    private JPasswordField passwordField1;
    private JPasswordField passwordField2;
    private JComboBox<String> comboBox;
    private JTextField personIdTextField;
    private JTextField addressTextField;
    private JTextField emailTextField;
    private JButton submitButton;
    private JButton backButton;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
